﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace SlamCms.SharePoint.Migration
{
	public class MigratableView : IMigratable
	{
		public string Name { get; set; }
		public string UrlName { get; set; }
		public string ViewFields { get; set; }
		public string Query { get; set; }
		public uint RowLimit { get; set; }
		public bool Paged { get; set; }
		public bool Default { get; set; }
		public bool Replace { get; set; }


		public MigratableView(bool replace)
		{
			this.Replace = replace;
		}

		public MigratableView()
		{
			// By default all views exported will replace the existing ones.
			this.Replace = true;
		}

		#region Serialization


		public XElement ToXml()
		{
			XElement node = new XElement("View");
			node.Add(new XAttribute("Name", Name));
			node.Add(new XAttribute("UrlName", UrlName));
			node.Add(new XAttribute("ViewFields", ViewFields));
			node.Add(new XAttribute("Query", !String.IsNullOrEmpty(Query) ? System.Web.HttpUtility.HtmlEncode(Query) : String.Empty));
			node.Add(new XAttribute("RowLimit", RowLimit));
			node.Add(new XAttribute("Paged", Paged));
			node.Add(new XAttribute("Default", Default));
			node.Add(new XAttribute("Replace", Replace));
			return node;
		}

		public void FromXml(XElement element)
		{
			this.Name = element.Attribute("Name").Value;
			this.UrlName = element.Attribute("UrlName").Value;
			this.ViewFields = element.Attribute("ViewFields").Value;
			this.Query = System.Web.HttpUtility.HtmlDecode(element.Attribute("Query").Value);
			this.RowLimit = uint.Parse(element.Attribute("RowLimit").Value);
			this.Paged = bool.Parse(element.Attribute("Paged").Value);
			this.Default = bool.Parse(element.Attribute("Default").Value);
			this.Replace = bool.Parse(element.Attribute("Replace").Value);
		}

		#endregion

	}
}
